def phrase_file_extension ( ) :
    return '.fr'

def phrase_file_creation_time ( ) :
    return 'creation time: '

def phrase_file_modification_time ( ) :
    return 'modification time: '
    
def phrase_file_size ( ) :
    return 'size: '

class _create_knowledge_base_state :
    def __init__ ( self ) :
        self . processing = False
        self . name = None

class _copy_knowledge_base_state :
    def __init__ ( self ) :
        self . processing = False
        self . name = None

class _rename_knowledge_base_state :
    def __init__ ( self ) :
        self . processing = False
        self . name = None

class knowledge_base_directory :
    def __init__ ( self , mediator ) :
        self . _mediator = mediator
        self . _read_file_contents = None
        self . _list_files = None
        self . _create_knowledge_base_state = _create_knowledge_base_state ( )
        self . _copy_knowledge_base_state = _copy_knowledge_base_state ( )
        self . _rename_knowledge_base_state = _rename_knowledge_base_state ( )
    def read_file_reply ( self , name , contents ) :
        self . _read_file_contents = contents
    def list_files_reply ( self , files ) :
        self . _list_files = files
    def current_time_reply ( self , time ) :
        if self . _create_knowledge_base_state . processing :
            self . _create_knowledge_base_state . processing = False
            self . _mediator . create_file ( self . _get_file_name ( self . _create_knowledge_base_state . name ) , 
                [ phrase_file_creation_time ( ) + time 
                , phrase_file_modification_time ( ) + time
                , phrase_file_size ( ) + '0' 
                ] )
        if self . _copy_knowledge_base_state . processing :
            self . _copy_knowledge_base_state . processing = False
            self . _mediator . create_file ( self . _get_file_name ( self . _copy_knowledge_base_state . name ) ,
                [ self . _read_file_contents [ 0 ]
                , phrase_file_modification_time ( ) + time
                , self . _read_file_contents [ 2 ]
                ] )
        if self . _rename_knowledge_base_state . processing :
            self . _rename_knowledge_base_state . processing = False
            self . _mediator . delete_file ( self . _get_file_name ( self . _rename_knowledge_base_state . name ) )
    def create_knowledge_base ( self , name ) :
        self . _create_knowledge_base_state . processing = True
        self . _create_knowledge_base_state . name = name
        self . _mediator . current_time_request ( )
    def rename_knowledge_base ( self , name , new_name ) :
        if name != new_name :
            self . _rename_knowledge_base_state . processing = True
            self . _rename_knowledge_base_state . name = name
            self . _copy_knowledge_base ( name , new_name )
    def copy_knowledge_base ( self , name , new_name ) :
        self . _copy_knowledge_base ( name , new_name )
    def list_knowledge_bases_request ( self ) :
        bases = [ ]
        self . _mediator . list_files_request ( )
        for file_name in self . _list_files :
            if file_name . endswith ( phrase_file_extension ( ) ) :
                bases += [ file_name [ 0 : - len ( phrase_file_extension ( ) ) ] ]
        self . _mediator . list_knowledge_bases_reply ( bases )
    def knowledge_base_creation_time_request ( self , name ) :
        self . _mediator . read_file_request ( self . _get_file_name ( name ) )
        result = self . _read_file_contents [ 0 ] [ len ( phrase_file_creation_time ( ) ) : ]
        self . _mediator . knowledge_base_creation_time_reply ( result )
    def knowledge_base_modification_time_request ( self , name ) :
        self . _mediator . read_file_request ( self . _get_file_name ( name ) )
        result = self . _read_file_contents [ 1 ] [ len ( phrase_file_modification_time ( ) ) : ]
        self . _mediator . knowledge_base_modification_time_reply ( result )
    def knowledge_base_size_request ( self , name ) :
        self . _mediator . read_file_request ( self . _get_file_name ( name ) )
        result = self . _read_file_contents [ 2 ] [ len ( phrase_file_size ( ) ) : ]
        self . _mediator . knowledge_base_size_reply ( result )
    def _get_file_name ( self , knowledge_base_name ) :
        return knowledge_base_name + phrase_file_extension ( )
    def _copy_knowledge_base ( self , name , new_name ) :
        self . _copy_knowledge_base_state . processing = True
        self . _copy_knowledge_base_state . name = new_name
        self . _mediator . read_file_request ( self . _get_file_name ( name ) )
        self . _mediator . current_time_request ( )
